﻿namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.ComponentModel;
    using System.Security.Permissions;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Web.UI.Design;
    using ComponentModel.Design;
    /// <summary>
    /// BackgroundHeadControl Item对象。
    /// </summary>
    /// <remarks>BackgroundHeadControl Item对象。</remarks>
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ToolboxItem(false)]
    [ToolboxData("<{0}:HeadItem runat=server></{0}:HeadItem>")]
    [NonVisualControlAttribute()]
    [ParseChildren(true)]
    [PersistChildren(false)]
    public class BackgroundHeadControlItem : Panel, INamingContainer, IStateManager, ICompositeControlDesignerAccessor, IAutomaticLoadControl//, IParserAccessor//, IAttributeAccessor
    {
        /*
        #region IParserAccessor
        void IParserAccessor.AddParsedSubObject(object obj)
        {
            HeadItem lc = obj as HeadItem;
            if (lc != null)
            {
                this.Text = lc.Text.Trim();
                this.ID = lc.ID.Trim();
            }
        }
        #endregion
         */ 
        /*
        #region IAttributeAccessor
        System.Web.UI.AttributeCollection attrs;
        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new System.Web.UI.AttributeCollection Attributes
        {
            get
            {
                if (attrs != null)
                    return attrs;

                if (this._viewState == null)
                {
                    _viewState = new StateBag(true);
                    if (this._isTrackingViewState)
                        ((IStateManager)_viewState).TrackViewState();
                }

                return attrs = new System.Web.UI.AttributeCollection(_viewState);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        string IAttributeAccessor.GetAttribute(string key)
        {
            if (attrs == null)
                return null;

            return (string)Attributes[key];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        void IAttributeAccessor.SetAttribute(string key, string value)
        {
            Attributes[key] = value;
        }
        #endregion
         */ 
        #region ICompositeControlDesignerAccessor
        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        #endregion
        #region IStateManager Members
        private bool _isTrackingViewState;
        private StateBag _viewState;
        internal void SetDirty()
        {
            if (_viewState != null)
                _viewState.SetDirty(true);
        }
        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return _isTrackingViewState;
            }
        }

        void IStateManager.LoadViewState(object state)
        {
            if (state != null)
            {
                ((IStateManager)ViewState).LoadViewState(state);
            }
        }

        object IStateManager.SaveViewState()
        {
            object savedState = null;

            if (_viewState != null)
            {
                savedState = ((IStateManager)_viewState).SaveViewState();
            }
            return savedState;
        }

        void IStateManager.TrackViewState()
        {
            _isTrackingViewState = true;
            if (_viewState != null)
            {
                ((IStateManager)_viewState).TrackViewState();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected override StateBag ViewState
        {
            get
            {
                if (_viewState == null)
                {
                    _viewState = new StateBag(false);
                    if (_isTrackingViewState)
                    {
                        ((IStateManager)_viewState).TrackViewState();
                    }
                }
                return _viewState;
            }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.IsNullOrEmpty(this.ID) ? base.ToString() : this.ID;
        }
        /// <summary>
        /// 获取控件宽度。
        /// </summary>
        /// <remarks>获取控件宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(false)]
        internal new Unit Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }
        /// <summary>
        /// 获取控件高度。
        /// </summary>
        /// <remarks>获取控件高度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(false)]
        internal new Unit Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
            }
        }

        private LinkButton _linkButton;
        private Panel _topPanel;
        /// <summary>
        /// </summary>
        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            CreateControlHierarchy();
            ClearChildViewState();
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            this.Controls.Clear();
            if (_topPanel == null)
            {
                _topPanel = new Panel();
            }
            //_topPanel.ID = "TopPanel";
            if (_linkButton == null)
            {
                _linkButton = new LinkButton();
            } 
            _linkButton.ID = "LinkButton";
            /*
            _linkButton.Text = this.Text;
            _linkButton.CommandArgument = this.CommandArgument;
            _linkButton.CommandName = this.CommandName;
            _linkButton.Font.CopyFrom(this.Font);
            _linkButton.ForeColor = this.ForeColor;
            _linkButton.ToolTip = this.ToolTip;
             */ 
            this.Controls.Add(_topPanel);
            this.Controls.Add(this._linkButton);
        }
        /// <summary>
        /// 获取或设置命令名，该命令名与传递给 Expanded 事件的 Button 控件相关联。
        /// </summary>
        /// <remarks>获取或设置命令名，该命令名与传递给 Expanded 事件的 Button 控件相关联。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [DefaultValue("")]
        public string CommandName
        {
            get
            {
                object o = this.ViewState["CommandName"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["CommandName"] = value;
            }
        }
        /// <summary>
        /// 获取或设置可选参数，该参数与关联的 CommandName 一起被传递到 Expanded 事件。
        /// </summary>
        /// <remarks>获取或设置可选参数，该参数与关联的 CommandName 一起被传递到 Expanded 事件。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [DefaultValue("")]
        public string CommandArgument
        {
            get
            {
                object o = this.ViewState["CommandArgument"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["CommandArgument"] = value;
            }
        }

        /// <summary>
        /// 获取或设置按钮名称。
        /// </summary>
        /// <remarks>获取或设置按钮名称。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [DefaultValue("")]
        public string Text
        {
            get
            {
                object o = this.ViewState["Text"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["Text"] = value;
            }
        }
        Style _linkButtonFont;
        /// <summary>
        /// 获取按钮字体对象。
        /// </summary>
        /// <remarks>获取按钮字体对象。</remarks>
        /// <value>System.Web.UI.WebControls.FontInfo</value>
        [Browsable(true)]
#if ONLY_1_1
		[DefaultValue(null)]
#endif
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [NotifyParentProperty(true)]
        public override FontInfo Font
        {
            get
            {
                if (this._linkButtonFont == null)
                {
                    this._linkButtonFont = new Style(this.ViewState);
                    if (this._isTrackingViewState)
                    {
                        ((IStateManager)this._linkButtonFont).TrackViewState();
                    }
                }
                if (this._linkButtonFont.Font.Size.IsEmpty) this._linkButtonFont.Font.Size = new FontUnit("14px");
                return _linkButtonFont.Font;
            }
        }
        /// <summary>
        /// 获取按钮字体原色。
        /// </summary>
        /// <remarks>获取按钮字体原色。</remarks>
        /// <value>string</value>
        [Bindable(true)]
        [Category("Appearance")] 
        [Browsable(true)]
        [DefaultValue(typeof(Color), "#ffffff")]
        [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
        public override Color ForeColor
        {
            get
            {
                object o = this.ViewState["ForeColor"];
                if (o == null) return ColorTranslator.FromHtml("#ffffff");
                return (Color)o;
            }
            set
            {
                this.ViewState["ForeColor"] = value;
            }
        }
        /// <summary>
        /// 获取按钮对象。
        /// </summary>
        /// <remarks>获取按钮对象。</remarks>
        /// <value>System.Web.UI.WebControls.LinkButton</value>
        [Browsable(false)]
        public LinkButton LinkButton
        {
            get
            {
                if (_linkButton == null) _linkButton = new LinkButton();
                return this._linkButton;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            _linkButton.Text = this.Text;
            _linkButton.CommandArgument = this.CommandArgument;
            _linkButton.CommandName = this.CommandName;
            _linkButton.Font.CopyFrom(this.Font);
            _linkButton.ForeColor = this.ForeColor;
            _linkButton.ToolTip = this.ToolTip;
            _linkButton.PostBackUrl = this.PostBackUrl;
            base.OnPreRender(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }
        /// <summary>
        /// 获取按钮上边对象。
        /// </summary>
        /// <remarks>获取按钮上边对象。</remarks>
        /// <value>System.Web.UI.WebControls.Panel</value>
        [Browsable(false)]
        internal System.Web.UI.WebControls.Panel TopPanel
        {
            get
            {
                if (_topPanel == null) _topPanel = new Panel();
                return _topPanel;
            }
        }
        /// <summary>
        /// 获取或设置当鼠标指针悬停在 Web 服务器控件上时显示的文本。
        /// </summary>
        /// <remarks>获取或设置当鼠标指针悬停在 Web 服务器控件上时显示的文本。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [DefaultValue("")]
        public override string ToolTip
        {
            get
            {
                object o = this.ViewState["ToolTip"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["ToolTip"] = value;
            }
        }

        /// <summary>
        /// 获取或设置选中的控件。
        /// </summary>
        /// <remarks>获取或设置选中的控件。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [DefaultValue("")]
        [Editor(typeof(UrlEditor), typeof(UITypeEditor))]
        [SRDescription("Manage_Head_DataTextField", typeof(Properties.Resources))]
        public string Ascx
        {
            get
            {
                object o = this.ViewState["Ascx"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["Ascx"] = value;
            }
        }

        /// <summary>
        /// 获取或设置单击 LinkButton 控件时从当前页发送到的网页的 URL。
        /// </summary>
        /// <remarks>获取或设置单击 LinkButton 控件时从当前页发送到的网页的 URL。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [DefaultValue("")]
        [Editor(typeof(UrlEditor), typeof(UITypeEditor))]
        public string PostBackUrl
        {
            get
            {
                object o = this.ViewState["PostBackUrl"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["PostBackUrl"] = value;
            }
        }

        /// <summary>
        /// 获取或设置选中的控件是否为下级菜单控件。
        /// </summary>
        /// <remarks>获取或设置选中的控件是否为下级菜单控件。</remarks>
        /// <value>bool</value>
        [Browsable(true)]
        [Bindable(true)]
        [DefaultValue(false)]
        [Category("Behavior")]
        public bool IsMenu
        {
            get
            {
                object o = this.ViewState["IsMenu"];
                if (o == null) return false;
                return (bool)o;
            }
            set
            {
                this.ViewState["IsMenu"] = value;
            }
        }

        /// <summary>
        /// 获取或设置是否返回默认菜单。
        /// </summary>
        /// <remarks>获取或设置是否返回默认菜单。</remarks>
        /// <value>bool</value>
        [Browsable(true)]
        [Bindable(true)]
        [DefaultValue(false)]
        [Category("Behavior")]
        public bool IsDefaultMenu
        {
            get
            {
                object o = this.ViewState["IsDefaultMenu"];
                if (o == null) return false;
                return (bool)o;
            }
            set
            {
                this.ViewState["IsDefaultMenu"] = value;
            }
        }

        /// <summary>
        /// 获取或设置是否返回上一级菜单。
        /// </summary>
        /// <remarks>获取或设置是否返回上一级菜单。</remarks>
        /// <value>bool</value>
        [Browsable(true)]
        [Bindable(true)]
        [DefaultValue(false)]
        [Category("Behavior")]
        public bool IsMenuBack
        {
            get
            {
                object o = this.ViewState["IsMenuBack"];
                if (o == null) return false;
                return (bool)o;
            }
            set
            {
                this.ViewState["IsMenuBack"] = value;
            }
        }
    }
}